import { userList } from './data'
import Mock from 'mockjs'
import { isDateTimeInRange, getNowDateTime } from '../../utils'
export default [
  {
    url: '/system/user/create', // 新增
    method: 'post',
    response: data => {
      insert(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: '/system/user/update', // 修改
    method: 'put',
    response: data => {
      updateById(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: '/system/user/update-status', // 更新状态
    method: 'put',
    response: data => {
      updateStatusById(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: '/system/user/update-password', // 更新密码
    method: 'put',
    response: data => {
      updatePasswordById(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/system/user/delete' + '.*'), // 删除
    method: 'delete',
    response: params => {
      deleteById(params)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/system/user/detail' + '.*'), // 获取详情
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getInfoById(params)
      }
    }
  },
  {
    url: RegExp('/system/user/page' + '.*'), // 查询分页列表
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByPage(params)
      }
    }
  },
  {
    url: RegExp('/system/user/all' + '.*'), // 查询列表
    method: 'get',
    response: () => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByAll()
      }
    }
  },
  {
    url: RegExp('/system/user/simple-list' + '.*'), // 查询简化列表
    method: 'get',
    response: () => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListBySimple()
      }
    }
  },
  {
    url: RegExp('/system/user/export' + '.*'), // 导出
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByExport(params)
      }
    }
  },
  {
    url: RegExp('/system/user/import' + '.*'), // 导入
    method: 'post',
    response: data => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: {
          createUsernames: [],
          updateUsernames: [],
          failureUsernames: []
        }
      }
    }
  }
]

const getListByPage = params => {
  let result = userList
  result = result.filter(item => !item.deleted)
  if (params.query.status) {
    result = result.filter(item => item.status === parseInt(params.query.status))
  }
  if (params.query.mobile) {
    result = result.filter(item => item.mobile.indexOf(params.query.mobile) !== -1)
  }
  if (params.query.nickname) {
    result = result.filter(item => item.nickname.indexOf(params.query.nickname) !== -1)
  }
  if (params.query.departId) {
    result = result.filter(item => item.departId === parseInt(params.query.departId))
  }
  if (params.query.create_time && params.query.create_time.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.create_time[0], params.query.create_time[1], item.create_time))
  }
  return {
    list: result.slice(parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1), parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1) + parseInt(params.query.pageSize)),
    total: result.length
  }
}

const getListByAll = () => {
  return userList
    .filter(item => item.status === 0 && !item.deleted)
    .map(item => ({
      id: item.id,
      username: item.username,
      nickname: item.nickname,
      avatar: item.avatar,
      departId: item.departId,
      departName: item.departName,
      roleIds: item.roleIds,
      postIds: item.postIds,
      mobile: item.mobile,
      email: item.email,
      sex: item.sex,
      status: item.status,
      remark: item.remark
    }))
}

const getListBySimple = () => {
  return userList
    .filter(item => item.status === 0 && !item.deleted)
    .map(item => ({
      id: item.id,
      username: item.username,
      nickname: item.nickname,
      avatar: item.avatar,
      departId: item.departId,
      departName: item.departName,
      roleIds: item.roleIds,
      postIds: item.postIds,
      mobile: item.mobile,
      email: item.email,
      sex: item.sex,
      status: item.status,
      remark: item.remark
    }))
}

const getListByExport = params => {
  let result = userList
  result = result.filter(item => !item.deleted)
  if (params.query.status) {
    result = result.filter(item => item.status === parseInt(params.query.status))
  }
  if (params.query.mobile) {
    result = result.filter(item => item.mobile.indexOf(params.query.mobile) !== -1)
  }
  if (params.query.nickname) {
    result = result.filter(item => item.nickname.indexOf(params.query.nickname) !== -1)
  }
  if (params.query.departId) {
    result = result.filter(item => item.departId === parseInt(params.query.departId))
  }
  if (params.query.create_time && params.query.create_time.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.create_time[0], params.query.create_time[1], item.create_time))
  }
  return result
}

const insert = data => {
  userList.unshift({
    ...data.body,
    id: Mock.Random.id(),
    creator: '1',
    create_time: getNowDateTime(),
    updater: '',
    update_time: '',
    deleted: false,
    deleted_time: ''
  })
}

const updateById = data => {
  userList.forEach(item => {
    if (item.id === data.body.id) {
      Object.keys(data.body).forEach(v => {
        item[v] = data.body[v]
      })
      item.updater = '1'
      item.update_time = getNowDateTime()
    }
  })
}

const updateStatusById = data => {
  userList.forEach(item => {
    if (item.id === data.body.id) {
      Object.keys(data.body).forEach(v => {
        item[v] = data.body[v]
      })
      item.updater = '1'
      item.update_time = getNowDateTime()
    }
  })
}

const updatePasswordById = data => {
  userList.forEach(item => {
    if (item.id === data.body.id) {
      Object.keys(data.body).forEach(v => {
        item[v] = data.body[v]
      })
      item.updater = '1'
      item.update_time = getNowDateTime()
    }
  })
}

const deleteById = params => {
  userList.forEach(item => {
    if (item.id === parseInt(params.query.id)) {
      item.deleted = true
      item.updater = '1'
      item.update_time = getNowDateTime()
      item.deleted_time = getNowDateTime()
    }
  })
}

const getInfoById = params => {
  const result = userList.filter(item => {
    return item.id === parseInt(params.query.id)
  })
  if (result.length > 0) {
    return result[0]
  } else {
    return null
  }
}
